﻿// See https://aka.ms/new-console-template for more information
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Polly;
using Serilog;
using Serilog.Events;
using Serilog.Filters;
using SqlSugar;
using System.Text;
using XjjXmm.FrameWork.Cache;
using XjjXmm.Spider;
using XjjXmm.Spider.biquge;

const string outputTemplate = "[{Timestamp:HH:mm:ss} {Level:u3}] ({SourceContext}:{ThreadId}){Message:lj}{NewLine}{Exception}";

//Log.Logger = new LoggerConfiguration()
//            .MinimumLevel.Debug()

//            //.Enrich.WithProperty("SourceContext", null) //加入属性SourceContext，也就运行时是调用Logger的具体类
//            .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
//            .Enrich.FromLogContext()
//            .Enrich.WithThreadId()
//            .Enrich.WithThreadName()
//            .WriteTo.Console(outputTemplate: outputTemplate)
//            .CreateLogger();
//            .WriteTo.Logger(lc => lc.Filter.ByExcluding(Matching.FromSource<Bige7ComSpider>())
//                 .WriteTo.File("log/Bige7ComSpider.txt"))
//            .WriteTo.Logger(lc => lc.Filter.ByExcluding(Matching.FromSource<BiqugeBizSpider>())
//                 .WriteTo.File("log/BiqugeBizSpider.txt"))
//            .WriteTo.Logger(lc => lc.Filter.ByExcluding(Matching.FromSource<BiqugeBizSpider>())
//                 .WriteTo.File("log/BiqugeBizSpider.txt"))
//            .WriteTo.Logger(lc => lc.Filter.ByExcluding(Matching.FromSource<BiqugeBizSpider>())
//                 .WriteTo.File("log/BiqugeBizSpider.txt"))
//            .WriteTo.Logger(lc => lc.Filter.ByExcluding(Matching.FromSource<BiqugeBizSpider>())
//                 .WriteTo.File("log/BiqugeBizSpider.txt"))
//.WriteTo.Logger(lc=>lc.Filter
//    .ByExcluding(pre=>pre.Level == LogEventLevel.Information)
//    .WriteTo.File("log/info.txt", rollingInterval: RollingInterval.Day)
//    )
//.WriteTo.Logger(lc => lc.Filter
//    .ByExcluding(pre => pre.Level == LogEventLevel.Debug)
//    .WriteTo.File("log/info.txt", rollingInterval: RollingInterval.Day)
//    )
//.WriteTo.File("log/log.txt", rollingInterval: RollingInterval.Day)
//
#if DEBUG
var config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.development.json", false, true).Build();
#else
var config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.prod.json", false, true).Build();
#endif

Setting.SiteSettings = config.GetSection("site").Get<List<SiteSetting>>();
RegistHelper.RegistLog();
var connectonString = "Server=192.168.88.25; Port=3306; Database=novel; Uid=root; Pwd=123456; Charset=utf8mb4;";
var sqlSugar = new SqlSugarScope(new ConnectionConfig()
{
    DbType = SqlSugar.DbType.MySql,
    ConnectionString = config["db:connectionString"],
    IsAutoCloseConnection = true,
},
    db =>
    {
        //单例参数配置，所有上下文生效
        db.Aop.OnLogExecuting = (sql, pars) =>
        {
            //App.Logger.Debug($"Sql:{sql}\r\n paramters: {JsonConvert.SerializeObject(pars)}");
            //Console.WriteLine(sql);//输出sql
            //Console.WriteLine(string.Join(",", pars?.Select(it => it.ParameterName + ":" + it.Value)));//参数
        };
    }
 );
//sqlSugar.DbMaintenance.CreateDatabase();
//sqlSugar.CodeFirst.InitTables(typeof(NovelEntity));
//sqlSugar.CodeFirst.InitTables(typeof(Chapter));

var builder = new HostBuilder();

builder.ConfigureServices((hostContext, services) =>
           {

               RegistHelper.RegistSpider(services);



               services.AddSingleton<ISqlSugarClient>(sqlSugar);

               services.AddSingleton<ExportService>();
               // services.AddSingleton<ILogger>(Log.Logger);

           }).UseConsoleLifetime();


var host = builder.UseSerilog().Build();
       
//await RedisClient.Instance.Init("192.168.88.25:6379");
//await RedisClient.Instance.Init("192.168.1.244:6379");


Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

RegistHelper.RunSpider(host.Services);
//RegistHelper.Export(host.Services);
//var myService = host.Services.GetServices<ISpider>().ToList();
//myService[0].Run();
//myService[1].Run();
//myService[2].Run();
//myService[3].Run();


host.Run();
